/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//TODO: Daten übernehmen...wie auch immer

package fwdisp.gui.tabs.settings;

import fwdisp.gui.moduls.IDTable;
import fwdisp.gui.moduls.SecRowPanel;
import fwdisp.gui.tabs.settings.ausrueckPanelElements.AusrueckAddFrame;
import java.awt.event.WindowEvent;
import settings.MainSettingsManager;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import settings.collections.AusrueckordnungCollection;
import settings.collections.ETypeCollection;
import settings.collections.elements.AusrueckOrdnung;
import settings.collections.elements.Car;

/**
 *
 * @author rbs
 */
public class AusrueckSettingsPanel extends SecRowPanel{

    private static JComboBox eTypComboBox;
    private IDTable table;
    private static JComboBox carComboBox;
    
    public AusrueckSettingsPanel() {
        
        
        final DefaultTableModel dtm = new DefaultTableModel(new String[]{"ID", "Fahrzeug"}, 0);
        
        eTypComboBox = new JComboBox();
        eTypComboBox.addItemListener(new ItemListener() {

            public void itemStateChanged(ItemEvent e) {
                if(eTypComboBox.getSelectedIndex() < 0)
                    return;
                AusrueckordnungCollection ausrueckordnungCollection = MainSettingsManager.getSetting().getAusrueckordnungCollection();
                dtm.setRowCount(0);
                
                if(eTypComboBox.getSelectedIndex() >= 0)
                {
                    AusrueckOrdnung aRO = ausrueckordnungCollection.getARO(eTypComboBox.getSelectedIndex());
                    ArrayList<Car> cars = aRO.getCarCollection().getCars();
                    for(Car car:cars)
                    {
                        String[] row = {"" + (dtm.getRowCount()), car.getName()};
                        dtm.addRow(row);
                    }

                    updateCars();
                }
            }
        });
                
        
        
        carComboBox = new JComboBox();


        updateAusrueckTypes(); //must be first!!!
        //Load Data for first E-Typ
        if(eTypComboBox.getItemCount() > 0)
            eTypComboBox.setSelectedIndex(0);
        
        updateCars();

        
        
        table = new IDTable(dtm);
        table.setEnabled(false);
        
        JButton addButton = new JButton("+");
        addButton.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e) {
                if (carComboBox.getItemCount() != 0)
                {
                       dtm.addRow(new String[]{"" + (dtm.getRowCount()), carComboBox.getSelectedItem().toString().split(" - ")[1]});
                    //System.out.println("added Car:" + Integer.parseInt(carComboBox.getSelectedItem().toString().split(" - ")[0]) + " getted from: " + carComboBox.getSelectedItem().toString().split(" - ")[0]);
                    Car used_car = MainSettingsManager.getSetting().getCarCollection().getCar(Integer.parseInt(carComboBox.getSelectedItem().toString().split(" - ")[0]));
                    AusrueckOrdnung aRO = MainSettingsManager.getSetting().getAusrueckordnungCollection().getARO(eTypComboBox.getSelectedIndex());
                    aRO.getCarCollection().addCar(used_car);
                    carComboBox.removeItemAt(carComboBox.getSelectedIndex());
                }
            }
        });
        
        JButton subButton = new JButton("-");

        subButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if( dtm.getRowCount() > 0)
                {
                    dtm.removeRow(dtm.getRowCount() -1);
                    AusrueckOrdnung aRO = MainSettingsManager.getSetting().getAusrueckordnungCollection().getARO(eTypComboBox.getSelectedIndex());
                    Car removing_car = aRO.getCarCollection().getCars().get(aRO.getCarCollection().getCars().size() - 1);
                    aRO.getCarCollection().getCars().remove(removing_car);
                    //carComboBox.addItem(removing_car.getId() + " - " + removing_car.getName());
                    updateCars();
                }
            }
        });
                
        JButton addAusrueckButton = new JButton("+");
        JButton editAusrueckButton = new JButton("Bearbeiten");
        editAusrueckButton.setToolTipText("Bearbeiten");
        JButton delAusrueckButton = new JButton("-");
        
        addAusrueckButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                AusrueckAddFrame ausrueckAddPanel = new AusrueckAddFrame();
                
                ausrueckAddPanel.addWindowListener(new WindowListener() {

                    public void windowOpened(WindowEvent e) {}

                    public void windowClosing(WindowEvent e) {}

                    public void windowClosed(WindowEvent e) {
                        AusrueckOrdnung aRO = AusrueckAddFrame.getARO();
                        MainSettingsManager.getSetting().getAusrueckordnungCollection().addARO(aRO);
                        eTypComboBox.addItem(aRO.getId() + " - " + aRO.getName());
                        eTypComboBox.setSelectedIndex(eTypComboBox.getItemCount() - 1);
                    }

                    public void windowIconified(WindowEvent e) {}

                    public void windowDeiconified(WindowEvent e) {}

                    public void windowActivated(WindowEvent e) {}

                    public void windowDeactivated(WindowEvent e) {}
                });
            }
        });
        
        editAusrueckButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (eTypComboBox.getSelectedIndex() >= 0)
                {
                    AusrueckAddFrame ausrueckAddPanel = new AusrueckAddFrame(MainSettingsManager.getSetting().getAusrueckordnungCollection().getARO(eTypComboBox.getSelectedIndex()));

                    ausrueckAddPanel.addWindowListener(new WindowListener() {

                        public void windowOpened(WindowEvent e) {}

                        public void windowClosing(WindowEvent e) {}

                        public void windowClosed(WindowEvent e) {
                            AusrueckOrdnung aRO = AusrueckAddFrame.getARO();
                            aRO.setId(eTypComboBox.getSelectedIndex());
                            aRO.setCars(MainSettingsManager.getSetting().getAusrueckordnungCollection().getARO(eTypComboBox.getSelectedIndex()).getCarCollection());
                            MainSettingsManager.getSetting().getAusrueckordnungCollection().setARO(eTypComboBox.getSelectedIndex(), aRO);
                            updateAusrueckTypes();
                        }

                        public void windowIconified(WindowEvent e) {}

                        public void windowDeiconified(WindowEvent e) {}

                        public void windowActivated(WindowEvent e) {}

                        public void windowDeactivated(WindowEvent e) {}
                    });
                }
            }
        });

        delAusrueckButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                MainSettingsManager.getSetting().getAusrueckordnungCollection().delARO(eTypComboBox.getSelectedIndex());
                updateAusrueckTypes();
            }
        });
                
        
        JPanel addPanel = new JPanel(new GridLayout(2, 2, 10, 20));
        JPanel ausrueckTypeButtonsPanel = new JPanel(new GridLayout(1, 3, 10, 10));
        JPanel ausrueckPanel = new JPanel(new GridLayout(2, 1, 5, 5));
        
        ausrueckTypeButtonsPanel.add(addAusrueckButton);
        ausrueckTypeButtonsPanel.add(delAusrueckButton);
        ausrueckTypeButtonsPanel.add(editAusrueckButton);
        ausrueckTypeButtonsPanel.setBackground(getBackground());
        
        ausrueckPanel.setBackground(getBackground());
        ausrueckPanel.add(eTypComboBox);
        ausrueckPanel.add(ausrueckTypeButtonsPanel);
        
        
        addPanel.add(carComboBox);
        addPanel.add(addButton);
        addPanel.add(new JLabel());
        addPanel.add(subButton);
        addPanel.setBackground(getBackground());
        
        setName("Ausrück- \nOrdnung");
        setVisible(true);
        
        
        this.setLayout(new BorderLayout(40, 40));
        this.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        add(ausrueckPanel, BorderLayout.NORTH);
        add(new JScrollPane(table), BorderLayout.CENTER);
        add(addPanel, BorderLayout.SOUTH);
    }
   
    public static void updateCars() {
        carComboBox.removeAllItems();
        ArrayList<Car> cars = MainSettingsManager.getSetting().getCarCollection().getCars();
        ETypeCollection eTypeCollection = MainSettingsManager.getSetting().geteTypeCollection();
        int aroID = eTypComboBox.getSelectedIndex();
        if (aroID >= 0)
        {
            ArrayList<Car> used_cars = MainSettingsManager.getSetting().getAusrueckordnungCollection().getARO(aroID).getCarCollection().getCars();
            for (Car car: cars)
            if (!used_cars.contains(car))
                carComboBox.addItem(car.getId() + " - " + car.getName());
        }
        
    }
    
    
    public static void updateAusrueckTypes() {
    
        eTypComboBox.removeAllItems();
        ArrayList<AusrueckOrdnung> aROs = MainSettingsManager.getSetting().getAusrueckordnungCollection().getAROs();
        
        for (AusrueckOrdnung ordnung: aROs)
            eTypComboBox.addItem(ordnung.getId() + " - " +  ordnung.getName());
    }
    

}
